package com.codeyang.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取地址工具类
 *
 * @author 雷哥
 */
public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL = "https://ip.taobao.com/outGetIpInfo";

    public static final String ACCESSKEY="alibaba-inc";

    public static String getRealAddressByIP(String ip) {
        String address = "XX XX";
        // 内网不查询
//        if (IpUtils.internalIp(ip)) {
//            return "内网IP";
//        }

        String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip+"&accessKey="+ACCESSKEY);
        if (StringUtils.isEmpty(rspStr)) {
            log.error("获取地理位置异常 {}", ip);
            return address;
        }
        System.out.println(rspStr);
        JSONObject obj = JSONObject.parseObject(rspStr);
        JSONObject data = obj.getObject("data", JSONObject.class);
        String region = data.getString("region");
        String city = data.getString("city");
        address = region + " " + city;
        return address;
    }

    public static void main(String[] args) {
        String realAddressByIP = getRealAddressByIP("219.139.204.181");
        System.out.println(realAddressByIP);
    }
}